/**
 * $Id$
 *
 * @author Noritaka Horio<holy.shared.design@gmail.com>
 * @copyright 2009 Noritaka Horio all rights resrved. 
 * @license The MIT License
 * @version $Revision$
 * @since
 */

var Scheduler = new Class({

	Implements: [Events],

	initialize: function(context) {
		this.timer = null;
		this.context = context;
	},

	getTime: function(){
		var date = new Date();
		var h = date.getHours();
		var m = date.getMinutes();

		var hourValue = (h >= 10) ? String(h) : "0" + String(h);
		var minuteValue = (m >= 10) ? String(m) : "0" + String(m);

		var timeFormat = hourValue + ":" + minuteValue;

		return timeFormat;
	},

	getDate: function(){
		var date = new Date();
		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		var day = date.getDate();

		var dateFormat = year + "-" + month + "-" + day;

		return dateFormat;
	},

	checkTime: function() {
		var date = this.getDate();
		var time = this.getTime();

		if (this.context.has(time)) {
			var task = this.context.get(time);
			if ($type(task) == "function") {
				task(date, time);
			} else {
				task.each(function(func, key) {func(date, time); });
			}
		}
		this.fireEvent("time", [this.getDate(), this.getTime()]);
	},

	stop: function() {
		$clear(this.timer);
		this.fireEvent("stop", [this.getDate(), this.getTime()]);
	},

	start: function() {
		this.timer = this.checkTime.periodical(60000, this);
		this.fireEvent("start", [this.getDate(), this.getTime()]);
	}
});